package com.minimalist.lindi.listener;

import com.minimalist.lindi.service.LindiIndexService;
import com.minimalist.lindi.service.LindiProvinceIndexService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * Spring Boot应用启动事件监听器
 * 用于在应用启动完成后执行一些初始化操作
 *
 * @author minimalist
 */
@Component
public class LindiApplicationStartupListener implements ApplicationListener<ApplicationStartedEvent>{

    private static final Logger log = LoggerFactory.getLogger(LindiApplicationStartupListener.class);


    @Resource
    private LindiIndexService lindiIndexService;

    @Resource
    private LindiProvinceIndexService lindiProvinceIndexService;

    @Override
    public void onApplicationEvent(@NotNull ApplicationStartedEvent event){
        log.info("应用启动完成，开始执行初始化操作");

        // 在这里可以添加应用启动后需要执行的初始化逻辑
        // 例如：加载配置、初始化资源、预热缓存等

        initializeApplicationResources();

        log.info("应用初始化操作执行完毕");
    }

    /**
     * 初始化应用资源
     */
    private void initializeApplicationResources(){
        // 这里实现具体的初始化逻辑
        log.info("正在初始化应用资源...");

        lindiIndexService.initTownIndex();
        lindiProvinceIndexService.initProvinceIndex();
    }
}